#pragma once

#include "Primitive.h"

class Rectangle: public Primitive
{
private:
	float x1, y1;
	float x2, y2;
	float x3, y3;
	float x4, y4;
	float normal[3];
	float sizeS;
	float sizeT;
public:
	Rectangle() 
	{
		normal[0] = 0.0;
		normal[1] = 0.0;
	};
	void calculateCoordinates(float _x1, float _y1, float _x2, float _y2){
		if(_y1 < _y2)
		{
			//Ponto 1
			x1 = _x1;
			y1 = _y1;
			//Ponto 2
			x2 = _x2;
			y2 = _y1;
			//Ponto3
			x3 = _x2;
			y3 = _y2;
			//Ponto 4
			x4 = _x1;
			y4 = _y2;
			normal[2] = 1.0;
		}
		else
		{
			//Ponto 1
			x1 = _x1;
			y1 = _y1;
			//Ponto 2
			x2 = _x1;
			y2 = _y2;
			//Ponto3
			x3 = _x2;
			y3 = _y2;
			//Ponto 4
			x4 = _x2;
			y4 = _y1;
			normal[2] = -1.0;
		}

		sizeS = abs(_x2-_x1);
		sizeT = abs(_y2-_y1);
	}

	void render()
	{
		if(!getNoTex())
		{
			glBindTexture(GL_TEXTURE_2D, getTex()->getTexID());
			glEnable(GL_TEXTURE_2D);
			glBegin(GL_POLYGON);
				glNormal3d(normal[0],normal[1],normal[2]);
				glTexCoord2f(0.0, 0.0); glVertex3d(x1,y1,0.0);
				glTexCoord2f(sizeS/getTex()->getLengthS(), 0.0); glVertex3d(x2,y2,0.0);
				glTexCoord2f(sizeS/getTex()->getLengthS(),  sizeT/getTex()->getLengthT()); glVertex3d(x3,y3,0.0);
				glTexCoord2f(0.0,  sizeT/getTex()->getLengthT()); glVertex3d(x4,y4, 0.0);
			glEnd();
			glDisable(GL_TEXTURE_2D);
		}
		else
		{
			glBegin(GL_POLYGON);
				glNormal3d(1.0,0.0,0.0);
				glVertex3d(x1,y1,0.0);
				glVertex3d(x2,y2,0.0);
				glVertex3d(x3,y3,0.0);
				glVertex3d(x4,y4, 0.0);
			glEnd();
		}
	}
};